package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.File;
import org.rcloud.io.model.Ruralfile;
import org.rcloud.io.service.RuralfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/21.
 */
@RestController
@RequestMapping(value = "/Ruralfile")
public class RuralfileController {

    @Autowired
    private RuralfileService ruralfileService;

    /*
  * 保存文件上传信息
  * */
    @ApiOperation(value="保存文件上传信息", notes="保存文件上传信息")
    @ResponseBody
    @RequestMapping(value = "/saveFile", produces = {"application/json;charset=UTF-8"})
    public ResultVo saveFile(@RequestBody String params){

        JSONObject object = JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());
        String eid = map.get("id").toString();
        String picture = map.get("picture").toString();
        String explain = map.get("explain").toString();

        Date t = new Date();
        Timestamp createData = new Timestamp(t.getTime());

        Ruralfile ruralfile = new Ruralfile();
        ruralfile.setEid(eid);
        ruralfile.setPicture(picture);
        ruralfile.setShouming(explain);
        ruralfile.setCreatedata(createData);
        ruralfile.setFlag(0);

        return  ruralfileService.save(ruralfile);
    }

    /*
  * 根据eid查询图片
  * */
    @ApiOperation(value="根据eid查询图片", notes="根据eid查询图片")
    @ResponseBody
    @RequestMapping(value = "/findFile", produces = {"application/json;charset=UTF-8"})
    public PageVo<Ruralfile> findFile(@RequestBody String params){

        String[] ids = params.split("=");
        return ruralfileService.findFileByNum(ids[1]);
    }
    /*
    * 根据id修改图片编码
    * */
    @ApiOperation(value="根据id修改图片编码", notes="根据id修改图片编码")
    @ResponseBody
    @RequestMapping(value = "/updateFile", produces = {"application/json;charset=UTF-8"})
    public ResultVo UpdateFile(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        return ruralfileService.updateFile(id);

    }
    /*
       * 修改全部图片flag
       * */
    @ApiOperation(value="修改全部图片flag", notes="修改全部图片flag")
    @ResponseBody
    @RequestMapping(value = "/updateFlag", produces = {"application/json;charset=UTF-8"})
    public ResultVo UpdateFlag(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        //根据id查询图片信息的eid
        Ruralfile ruralfile = (Ruralfile) ruralfileService.findFileById(id).getInfo();
        String eid = ruralfile.getEid();

        //根据eid修改所有的flag
        int f = Integer.valueOf(ruralfileService.updateFlagAll(eid).getInfo().toString());
        System.out.println("+++++"+f+"----------");

        return ruralfileService.updateFlag(id);

    }
}
